//
//  TTMineViewCell.m
//  TaoTuo
//
//  Created by admin on 2019/10/17.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTMineViewCell.h"
#import "TTMineFormViewCell.h"

#define itemH 55
#define rowCount 5.0

static NSString *cellID = @"mine_form_cell_id";

@interface TTMineViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UIView *bgView;   // 背景view
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *rightBtn;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UICollectionView *collectionView;

@end


@implementation TTMineViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.clipsToBounds = YES;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [self titleLabel];
        [self rightBtn];
        [self collectionView];
        
    }
    return self;
}


- (void)setModel:(TTMineViewModel *)model
{
    _model = model;
    self.titleLabel.text = model.sectionTitle;
    
    self.rightBtn.hidden = model.btnTitle.length == 0;
    if (model.btnTitle.length > 0) {
        if (model.target && model.action) {
            [self.rightBtn addTarget:model.target action:model.action forControlEvents:UIControlEventTouchUpInside];
        }
        
        [self.rightBtn setTitle:model.btnTitle forState:UIControlStateNormal];
        CGFloat s = 0.5;
        self.rightBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -self.rightBtn.imageView.width - s , 0, self.rightBtn.imageView.width + s);
        self.rightBtn.imageEdgeInsets = UIEdgeInsetsMake(0, self.rightBtn.titleLabel.width + s, 0, -self.rightBtn.titleLabel.width - s);
    }
    
    [self.collectionView reloadData];
    
    NSInteger row = ceil(model.itemModelArray.count / 5.0);
    CGRect rect = CGRectMake(0, 0, SCREEN_WIDTH - 20, row * 60 + 50);
//    self.bgView.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:10].CGPath;
//    [self layoutIfNeeded];
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:model.corner cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.fillColor = UIColor.whiteColor.CGColor;
    layer.path = path.CGPath;
    [self.bgView.layer insertSublayer:layer atIndex:0];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.model.itemModelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TTMineItemModel *model = self.model.itemModelArray[indexPath.row];
    TTMineFormViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.model = model;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    TTMineItemModel *model = self.model.itemModelArray[indexPath.row];
    
    if ([self.delegate respondsToSelector:@selector(mineViewCell:selectItem:index:)]) {
        [self.delegate mineViewCell:self selectItem:model index:indexPath.row];
    }
    
}


- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
//        _bgView.backgroundColor = [UIColor whiteColor];
//        _bgView.layer.cornerRadius = 10;
//        _bgView.layer.shadowColor = kMainColor.CGColor;
//        _bgView.layer.shadowOffset = CGSizeZero;
//        _bgView.layer.shadowOpacity = 0.2;
        [self.contentView addSubview:_bgView];
        [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.left.mas_equalTo(10);
            make.right.equalTo(self.contentView).mas_offset(-10);
            make.bottom.equalTo(self.contentView).mas_offset(-0);
        }];
    }
    return _bgView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:16];
        [self.bgView addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bgView).mas_offset(5);
            make.left.mas_equalTo(10);
            make.height.mas_equalTo(34);
        }];
    }
    return _titleLabel;
}

- (UIButton *)rightBtn
{
    if (!_rightBtn) {
        _rightBtn = [UIButton createButtonWithFrame:CGRectMake(self.width - 100, 0, 100, self.height) image:[UIImage imageNamed:@"btn_right_arrow"] title:@"" titleColor:UIColorHex(999999) titleFont:[UIFont systemFontOfSize:14] space:1 backgroundColor:nil target:nil action:nil];
        _rightBtn.isNeedLogin = YES;
        [self.bgView addSubview:_rightBtn];
        [_rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.height.equalTo(self.titleLabel);
            make.right.equalTo(self.bgView).mas_offset(-10);
        }];
    }
    return _rightBtn;
}

- (UIView *)lineView
{
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = UIColor.clearColor;
        [self.bgView addSubview:_lineView];
        [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom);
            make.left.mas_equalTo(10);
            make.right.mas_equalTo(-10);
            make.height.mas_equalTo(1);
        }];
    }
    return _lineView;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        
        CGFloat minimumInteritemSpacing = 3;
        CGFloat w = floor((SCREEN_WIDTH - 20 - (rowCount - 1) * minimumInteritemSpacing) / rowCount);
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(w, itemH);
        layout.minimumInteritemSpacing = minimumInteritemSpacing;
        layout.minimumLineSpacing = 5;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollEnabled = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[TTMineFormViewCell class] forCellWithReuseIdentifier:cellID];
        [self.bgView addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.lineView.mas_bottom).mas_offset(5);
            make.left.right.equalTo(self.bgView);
            make.bottom.equalTo(self.bgView).mas_offset(0);
        }];
    }
    return _collectionView;
}

@end
